//导入
import axios from 'axios';

import NProgress from 'nprogress'; // import NProgress from '../node_modules/nprogress/nprogress.js
import 'nprogress/nprogress.css';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

//创建实例对象
let instance = axios.create({
  baseURL: 'http://127.0.0.1:3001'
});

//为实例添加拦截器
instance.interceptors.request.use(config => {
  //显示进度条
  NProgress.start();
  //返回 config
  return config;
});

instance.interceptors.response.use(response => {
  //关闭进度条
  NProgress.done();
  //返回 config
  return response.data;
}, error => {
  //错误的统一处理
  toast.error('请求失败~~~');
  console.log(error);
  //返回一个 pending 状态的 promise
  return new Promise(() => {});
});

//暴露
export default instance;